/**
 * @filename:ParameterServiceImpl 2023-4-17
 * @project Wonder Java前后台分离框架  1.1.0
 * Copyright(c) 2022 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.admin.service.impl;

import com.wonder.admin.domain.Parameter;
import com.wonder.admin.mapper.ParameterDao;
import com.wonder.admin.service.ParameterService;
import com.wonder.admin.request.ParameterRequest;
import com.wonder.common.utils.StringUtils;
import com.wonder.framework.redis.RedisCache;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.common.AjaxResult;
import com.wonder.common.constants.Constants;

import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**   
 * @Description:TODO(参数配置服务实现)
 *
 * @version: 1.1.0
 * @author: Wonder
 * 
 */
@Service
public class ParameterServiceImpl  extends ServiceImpl<ParameterDao, Parameter> implements ParameterService  {

	@Autowired
	RedisCache redisCache;
    /**
     * 验证参数配置信息
     * 
     * @param parameter
     * @param type
     * @return String
     */
    @Override
    public String validate(Parameter parameter,String type){
    
    
       return "";
    }
    
    
    /**
	 * Description:参数配置分页返回
	 * @param request //检索条件
	 * @param page //分页参数
	 * @return IPage<Parameter>   //分页记录
	 */
	@Override 
	public IPage<Parameter> selectList(ParameterRequest request, Page<Parameter> page){
	    LambdaQueryWrapper<Parameter> lambdaQueryWrapper = new LambdaQueryWrapper<>();
	    
        if (StringUtils.hasText(request.getName())) {
			    lambdaQueryWrapper.like(Parameter::getName, request.getName()); 
		    } 
             
        if (StringUtils.hasText(request.getLanguage())) {
			    lambdaQueryWrapper.eq(Parameter::getLanguage, request.getLanguage());
		    } 
             
        
		Page<Parameter> selectPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);
		return selectPage;
    }
	
	/**
	 * 获取配置
	 * 
	 * @return
	 */
	@Override
	public String getKey(String key) {

		Parameter parameter=this.getOne(new QueryWrapper<Parameter>().eq("name", key));
		if(parameter!=null)return parameter.getValue();

		return null;

	}
	
	
	@Override
	public Map<String, Object> getParameterByKeyString(String keyString) {
		String parmList[] = keyString.split(",");
		Map<String, Object> response = new HashMap<>();
		if (parmList.length > 0) {
			for (String key : parmList) {
				Parameter redisParam =getCacheKey(key);	
				response.put(key, redisParam.getValue());
			}
		}
		return response;
	}

	/**
	 * 从Cache中获取配置值
	 * 
	 * @param key
	 * @return
	 */
	@Override
	public Parameter getCacheKey(String key) {

		List<Parameter> list = redisCache.getCacheObject(Constants.SYS_PARAMETER_KEY);
		if (list == null) {
			list = this.list();
			redisCache.setCacheObject(Constants.SYS_PARAMETER_KEY, list, 120, TimeUnit.SECONDS);
		}
		// this.list();

		for (Parameter item : list) {
			if(item.getName().equals(key)) return item;
		}
		

		return null;

	}

	/**
	 * 从Cache中获取配置值
	 * 
	 * @param key
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getBanners(String language,String namePrefix) {

		//check cache first
		List<Map<String, Object>> res = redisCache.getCacheObject(Constants.SYS_PARAMETER_KEY+"banners:"+language);
		if(res != null){
			return res;
		}

		List<Parameter> list = redisCache.getCacheObject(Constants.SYS_PARAMETER_KEY);
		if (list == null) {
			list = this.list();
			redisCache.setCacheObject(Constants.SYS_PARAMETER_KEY, list, 120, TimeUnit.SECONDS);
		}
		List<Map<String, Object>> response = new ArrayList<>();

		for (Parameter parameter : list) {
			if (parameter.getLanguage().equals(language) && parameter.getName().indexOf(namePrefix)>=0) {
				Map<String, Object> item = new HashMap<>();
				item.put("img", parameter.getValue());
				item.put("link", parameter.getExtend());

				response.add(item);
			}
		}

		redisCache.setCacheObject(Constants.SYS_PARAMETER_KEY+"banners:"+language,response,120,TimeUnit.SECONDS);

		return response;

	}
        
    
	
}